package cn.homework.dao.impl;

import cn.homework.dao.RegisterDao;
import cn.homework.domain.Admin;
import cn.homework.domain.Customers;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author ACT
 */
public class RegisterDaoImpl implements RegisterDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addRegisterCustomer(Customers customers) {
        String sql = "insert into customers values(?, ?, ?, ?, ?, ?, ?, ?)";
        template.update(sql, customers.getName(), customers.getPassword(), customers.getHeader(), customers.getPhone(),
                customers.getQuestion(), customers.getAnswer(), customers.getAddress(), customers.getEmail());
    }

    @Override
    public Customers queryRegisterCustomer(Customers customers) {
        String sql = "select * from customers where name = ?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<Customers>(Customers.class), customers.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Customers updateCustomerMaterial(Customers customers) {
        String sql = "update customers set password = ?, header = ?, phone = ?, question = ?, answer = ?, address = ?, email = ? where name = ?";
        template.update(sql, customers.getPassword(), customers.getHeader(), customers.getPhone(), customers.getQuestion(),
                customers.getAnswer(), customers.getAddress(), customers.getEmail(), customers.getName());
        return null;
    }

    @Override
    public Admin queryRegisterAdmin(Admin admin) {
        String sql = "select * from admin where name = ?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), admin.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void addRegisterAdmin(Admin admin) {
        String sql = "insert into admin values(?, ?, ?, ?, ?)";
        template.update(sql, admin.getName(), admin.getPassword(), admin.getHeader(),
                admin.getPhone(), admin.getEmail());
    }
}
